import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home/index.vue'),
    meta: { title: '首页' }
  },
  {
    path: '/news',
    name: 'News',
    component: () => import('@/views/News/index.vue'),
    meta: { title: '招生指南' }
  },
  {
    path: '/news/:id',
    name: 'NewsDetail',
    component: () => import('@/views/News/detail.vue'),
    meta: { title: '新闻详情' }
  },
  {
    path: '/my',
    name: 'My',
    component: () => import('@/views/My/index.vue'),
    meta: { title: '我的' }
  },
  {
    path: '/board',
    name: 'Board',
    component: () => import('@/views/Board/index.vue'),
    meta: { title: '表白墙' }
  },
  {
    path: '/board/add',
    name: 'BoardAdd',
    component: () => import('@/views/Board/add.vue'),
    meta: { title: '发布表白' }
  },
  {
    path: '/board/:id',
    name: 'BoardDetail',
    component: () => import('@/views/Board/Detail.vue'),
    meta: { title: '表白详情' }
  },
  {
    path: '/job',
    name: 'Job',
    component: () => import('@/views/Job/index.vue'),
    meta: { title: '兼职招聘' }
  },
  {
    path: '/job/add',
    name: 'JobAdd',
    component: () => import('@/views/Job/add.vue'),
    meta: { title: '发布兼职' }
  },
  {
    path: '/job/:id',
    name: 'JobDetail',
    component: () => import('@/views/Job/detail.vue'),
    meta: { title: '兼职详情' }
  },
  {
    path: '/lost',
    name: 'Lost',
    component: () => import('@/views/Lost/index.vue'),
    meta: { title: '失物招领' }
  },
  {
    path: '/lost/add',
    name: 'LostAdd',
    component: () => import('@/views/Lost/add.vue'),
    meta: { title: '发布失物' }
  },
  {
    path: '/lost/:id',
    name: 'LostDetail',
    component: () => import('@/views/Lost/detail.vue'),
    meta: { title: '失物详情' }
  },
  {
    path: '/trade',
    name: 'Trade',
    component: () => import('@/views/Trade/index.vue'),
    meta: { title: '二手交易' }
  },
  {
    path: '/trade/add',
    name: 'TradeAdd',
    component: () => import('@/views/Trade/add.vue'),
    meta: { title: '发布商品' }
  },
  {
    path: '/trade/detail/:id',
    name: 'TradeDetail',
    component: () => import('@/views/Trade/detail.vue'),
    meta: { title: '商品详情' }
  },
  {
    path: '/leave',
    name: 'Leave',
    component: () => import('@/views/Leave/index.vue'),
    meta: { title: '请假申请' }
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('@/views/About/index.vue'),
    meta: { title: '关于我们' }
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/Search/index.vue'),
    meta: { title: '搜索' }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/index.vue'),
    meta: { title: '登录', hideTabBar: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - 校园生活圈`
  }
  
  // 检查登录状态
  const token = localStorage.getItem('token')
  const isLoginPage = to.path === '/login'
  
  // 如果没有token且不是登录页面，跳转到登录页
  if (!token && !isLoginPage) {
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
    return
  }
  
  // 如果已登录且访问登录页，跳转到首页
  if (token && isLoginPage) {
    next('/')
    return
  }
  
  next()
})

export default router